//
//  YueController.m
//  Shengzhuli
//
//  Created by subook on 2017/10/10.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "YueController.h"
#import "YueKaCell.h"
#import "SeviceCell.h"
#import "SZLSinglePayViewController.h"
#import "YueKaNetWork.h"
#import "UIImage+Extension.h"
#import "MBProgressHUD+MJ.h"
#import "KaProductModel.h"
#import "SZLAccountHelper.h"
#import "SZLSinglePayViewController.h"
#import "SingleCreateModel.h"
#import <MJRefresh.h>

@interface YueController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic, strong)UIView *headerBackView;
@property (nonatomic, strong)UIImageView *portraitView;
@property (nonatomic, strong)UILabel *nameLabel;
@property (nonatomic, strong)UILabel *describeLabel;
@property (nonatomic, strong)UILabel *addressLabel;
@property (nonatomic, strong)UILabel *lineLabel;
@property (nonatomic, strong)UIButton *yueButton;

@property (nonatomic, strong)UIView *bottomView;
@property (nonatomic, strong)UIView *backView;

@property (nonatomic, strong)UILabel *payOrderLabel;
@property (nonatomic, strong)UILabel *desLabel;
@property (nonatomic, strong)UILabel *priceLabel;
@property (nonatomic, strong)UIButton *payButton;
@property (nonatomic, strong)UILabel *moneyLabel;
@property (nonatomic, strong)UILabel *redLineLabel;//红线

@property (nonatomic, assign) NSInteger pageNo;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) NSString *describe;
@property (nonatomic, strong) NSString *money;
@property (nonatomic, strong) NSString *macaId;
@property (nonatomic, strong) NSString *macaWorksId;
@property (nonatomic, strong) SingleCreateModel *singleModel;
@end

@implementation YueController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _pageNo = 1;
    _dataArray = [[NSMutableArray alloc] init];
    [self estabHeaderView];
    [self setUpTableView];
    [self estabFooterView];
    [self loadKaMessageData];
    [self loadKaProductData];
}
- (void)loadKaMessageData {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"id"] = self.ID;
    [YueKaNetWork getKaMessageWithParame:params response:^(BOOL success, NSString *msg,NSDictionary *dict) {
        if (success) {
            
            [_portraitView sd_setImageWithURL:[NSURL URLWithString:dict[@"representativeImag"]]];
            _nameLabel.text = dict[@"name"];
            _describeLabel.text = dict[@"introduce"];
            NSLog(@"%@",dict[@"introduce"]);
            UIFont *font= [UIFont fontWithName:dict[@"introduce"] size:15];
            CGSize size = [_describeLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil]];
            // 名字的H
            CGFloat nameH = size.height;
            // 名字的W
            CGFloat nameW = size.width+60;
            _describeLabel.frame = CGRectMake(ScreenW/2-nameW/2,160, nameW,nameH+10);
            _addressLabel.text = dict[@"address"];
            self.title = dict[@"name"];
        }else {
            [MBProgressHUD showError:msg toView:self.view];
        }
        
    }];
    
}
- (void)loadKaProductData {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"id"] = self.ID;
    params[@"pageNo"] = @(_pageNo);
    [YueKaNetWork getKaProductListWithParame:params response:^(BOOL success, NSString *msg, NSArray *arr) {
        if (success) {
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
            for (NSMutableDictionary *dict in arr) {
                KaProductModel *model = [[KaProductModel alloc] init];
                [model updateModelWithDict:dict];
                [_dataArray addObject:model];

                
            }
            
            [_tableView reloadData];
        }else {
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)estabFooterView {
    _yueButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _yueButton.frame = CGRectMake(0, ScreenH-64-50, ScreenW, 50);
    _yueButton.backgroundColor = [UIColor redColor];
    [_yueButton setTitle:@"立刻预约" forState:(UIControlStateNormal)];
    [_yueButton addTarget:self action:@selector(yuyueButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_yueButton];
}
- (void)yuyueButtonClick:(UIButton *)btn {
    if ([SZLAccountHelper shouldLogin]) {
        [SZLAccountHelper shouldLogin];
    }else {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideBackView)];
        
        _bottomView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, ScreenW, ScreenH-64-161))];
        _bottomView.backgroundColor = [UIColor grayColor];
        _bottomView.alpha = 0.5;
        [self.view addSubview:_bottomView];
        
        [_bottomView addGestureRecognizer:tap];
        _backView = [[UIView alloc] initWithFrame:(CGRectMake(0, ScreenH-64-161, ScreenW, 161))];
        _backView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_backView];
        
        _payOrderLabel = [[UILabel alloc] initWithFrame:(CGRectMake(0, 0, ScreenW, 40))];
        _payOrderLabel.text = @"支付订单";
        _payOrderLabel.textAlignment = NSTextAlignmentCenter;
        _payOrderLabel.textColor = [UIColor lightGrayColor];
        [_backView addSubview:_payOrderLabel];
        
        _redLineLabel = [[UILabel alloc] initWithFrame:(CGRectMake(0, 40, ScreenW, 1))];
        _redLineLabel.backgroundColor = [UIColor redColor];
        [_backView addSubview:_redLineLabel];
        
        _desLabel = [[UILabel alloc] initWithFrame:(CGRectMake(10, 41, ScreenW-20, 70))];
        _desLabel.text = self.describe;
        _desLabel.numberOfLines = 0;
        _desLabel.textColor = [UIColor redColor];
        [_backView addSubview:_desLabel];
        
        _moneyLabel = [[UILabel alloc] initWithFrame:(CGRectMake(0, 111, ScreenW/7*2, 50))];
        _moneyLabel.textColor = [UIColor redColor];
        _moneyLabel.textAlignment = NSTextAlignmentCenter;
        _moneyLabel.backgroundColor = [UIColor lightGrayColor];
        _moneyLabel.layer.borderWidth = 0.5;
        _moneyLabel.layer.borderColor = [UIColor grayColor].CGColor;
        _moneyLabel.text = [@"¥" stringByAppendingString:self.money];
        [_backView addSubview:_moneyLabel];
        
        _payButton =[UIButton buttonWithType:(UIButtonTypeCustom)];
        _payButton.frame = CGRectMake(ScreenW/7*2, 111, ScreenW/7*5, 50);
        _payButton.backgroundColor = [UIColor redColor];
        [_payButton setTitle:@"去支付" forState:(UIControlStateNormal)];
        [_payButton addTarget:self action:@selector(sureButtonClick) forControlEvents:(UIControlEventTouchUpInside)];
        [_backView addSubview:_payButton];
    }
    
    
}
- (void)hideBackView {
    _bottomView.hidden = YES;
    _backView.hidden = YES;
}
- (void)sureButtonClick {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"token"] = [SZLAccountHelper shareInstance].token;
    params[@"money"] = self.money;
    params[@"macaId"] = self.macaId;
    params[@"macaWorksId"] = self.macaWorksId;
    [YueKaNetWork getKaProductOrderWithParame:params response:^(BOOL success, NSString *msg, NSDictionary *dict) {
       
        if (success) {
            self.singleModel = [[SingleCreateModel alloc] init];
            SZLSinglePayViewController *payVc = [[SZLSinglePayViewController alloc] init];
            _singleModel.sumPrice = [dict[@"sumPrice"] doubleValue];
            _singleModel.payNumber = dict[@"payNumber"];
            payVc.model = self.singleModel;
            [self.navigationController pushViewController:payVc animated:YES];

        }else {
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)estabHeaderView {
    
    _headerBackView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, ScreenW, 260))];
    _portraitView = [[UIImageView alloc] initWithFrame:(CGRectMake(0, 0, ScreenW, 200))];
    
    [_headerBackView addSubview:_portraitView];
    
    _nameLabel = [[UILabel alloc] initWithFrame:(CGRectMake(ScreenW/2-50, 120, 100, 40))];
    [_nameLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:24]];
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    //_nameLabel.backgroundColor = RGBColor(87, 85, 85);
    _nameLabel.backgroundColor = [UIColor clearColor];
    _nameLabel.textColor = [UIColor whiteColor];
    [_headerBackView addSubview:_nameLabel];
    
    _describeLabel = [[UILabel alloc] initWithFrame:(CGRectMake(0, 160, ScreenW, 30))];
    _describeLabel.font = [UIFont systemFontOfSize:15];
    _describeLabel.textAlignment = NSTextAlignmentCenter;
    _describeLabel.backgroundColor = RGBColor(87, 85, 85);
    _describeLabel.textColor = [UIColor whiteColor];
    [_headerBackView addSubview:_describeLabel];
    
    _addressLabel = [[UILabel alloc] initWithFrame:(CGRectMake(ScreenW/2-50, 200, 100, 40))];
    _addressLabel.font = [UIFont systemFontOfSize:14];
    _addressLabel.textAlignment = NSTextAlignmentCenter;
    _addressLabel.backgroundColor = [UIColor whiteColor];
    _addressLabel.text = @"北京   朝阳";
    [_headerBackView addSubview:_addressLabel];
    
    _lineLabel = [[UILabel alloc] initWithFrame:(CGRectMake(0, 250, ScreenW, 10))];
    _lineLabel.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
    
    [_headerBackView addSubview:_lineLabel];
    
}
- (void)setUpTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, ScreenW, ScreenH)) style:(UITableViewStylePlain)];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH-64);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageNo = 1;
        [_dataArray removeAllObjects];
        [self loadKaProductData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _pageNo++;
        [self loadKaProductData];
    }];
    self.tableView.tableHeaderView = _headerBackView;
    [self.tableView registerClass:[SeviceCell class] forCellReuseIdentifier:NSStringFromClass([SeviceCell class])];
    [self.view addSubview:self.tableView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KaProductModel *model = _dataArray[indexPath.row];
    SeviceCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SeviceCell class])];
    [cell updateCellWithModel:model];
    if (indexPath.row == 0) {
        [self.tableView selectRowAtIndexPath:indexPath animated:NO  scrollPosition:UITableViewScrollPositionNone];
        cell.selectedBackgroundView.backgroundColor = [UIColor lightGrayColor];
        [self getLabelMessage:model];
    }
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.00001;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KaProductModel *model = _dataArray[indexPath.row];
    [self getLabelMessage:model];
    
    
}
- (void)getLabelMessage:(KaProductModel *)model {
    self.money = model.money;
    self.describe = model.headings;
    self.macaWorksId = model.macaWorksId;
    self.macaId = model.macaId;
}
- (CGSize)AutoSizeWithStr:(NSString *)str textfont:(UIFont *)textfont size:(CGSize)size {
    return [str boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine |
            NSStringDrawingUsesLineFragmentOrigin |
            NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:textfont} context:nil].size;
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
